package com.shaozi.nc.hibservice;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shaozi.data.hib.dao.BaseHibDao;
import com.shaozi.data.hib.service.BaseServiceImpl;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.utils.date.DateHelper;
import com.shaozi.utils.date.bean.DateBean;
import com.shaozi.nc.data.hibVO.AttendanceVO;

@Service(value = "attendanceService")
public class AttendanceService extends BaseServiceImpl<AttendanceVO> {

	@Autowired
	public AttendanceService(BaseHibDao<AttendanceVO> baseHibDao) {
		super(baseHibDao);
	}

	/**
	 * 获取今天未打卡
	 * 
	 * @return
	 * @throws SzException
	 */
	public List<AttendanceVO> getTodayUnpunch(String companyId, long levelId, int type) throws SzException {
		DateBean dateBean = DateHelper.getDateBean();
		return getDao().getDaoSupport()
				.queryObjects("from AttendanceVO a where  a.status=" + AttendanceVO.Status_UnOper + " and a.level_id="
						+ levelId + " and a.sys_insert_time>=" + (dateBean.getDayStart() / 1000)
						+ " and a.sys_insert_time<=" + (dateBean.getDayEnd() / 1000) + " and type=" + type
						+ " and companyId=" + companyId + " and  isDelete=0", -1, -1);
	}

	/**
	 * 考勤异常申诉，有一条算一条
	 * 
	 * @param companyId
	 * @return
	 * @throws SzException
	 */
	@SuppressWarnings("unchecked")
	public List<AttendanceVO> getAttendanceUserByGrouop(String companyId) throws SzException {
		String hql = "from AttendanceVO where companyId=" + companyId + " and status=" + AttendanceVO.Status_Abnormal
				+ " and isAppealExpire=0 and  isDelete=0 order by uid";
		return getDao().getDaoSupport().queryObjects(hql, 0, -1);
	}

	/**
	 * 考勤异常申诉
	 * 
	 * @param companyId
	 * @param uid
	 * @return
	 * @throws SzException
	 */
	public AttendanceVO getAttendanceComplains(String companyId, int id) throws SzException {
		String hql = "from AttendanceVO where status=" + AttendanceVO.Status_Abnormal + " and isAppealExpire=0 and id="
				+ id + " and companyId=" + companyId + " and isDelete=0";
		List<AttendanceVO> vos = getDao().getDaoSupport().queryObjects(hql, 0, -1);
		if (vos.isEmpty())
			return null;
		return vos.get(0);
	}

}
